#include "../edit/TextDoc.h"
#include "unittest.h"

TEST_GROUP (TestTextDoc)
{
}

/***************************** Test cases for CTextDoc ***********************/
TEST_CASE (TestTextDoc, test_StripCrLf) 
{
    char buf[1024];
    char *next, *cur;

    strcpy (buf, "Hello world.");
    cur = buf;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, "Hello world.") == 0);
    TEST_ASSERT (next == NULL);

    strcpy (buf, "Hello \r\nworld.");
    cur = buf;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, "Hello ") == 0);
    TEST_ASSERT (next == cur + 8);
    TEST_ASSERT (strcmp(next, "world.") == 0);
    cur = next;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, "world.") == 0);
    TEST_ASSERT (next == NULL);

    strcpy (buf, "Hello \r\nworld\r.\n");
    cur = buf;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, "Hello ") == 0);
    TEST_ASSERT (next == cur + 8);
    TEST_ASSERT (strcmp(next, "world\r.\n") == 0);
    cur = next;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, "world") == 0);
    TEST_ASSERT (next == cur+6);
    cur = next;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, ".") == 0);
    TEST_ASSERT (next == cur +2);
    cur = next;
    StripCrLf (cur, &next);
    TEST_ASSERT (strcmp(cur, "") == 0);
    TEST_ASSERT (next == NULL);
    
    strcpy (buf, "\r\n\r\r\n\n\n\r");
    int i = 0;
    cur = buf;
    while (cur) {
        StripCrLf(cur, &next);
        i ++;
        cur = next;
    }
    TEST_ASSERT (i == 6);

    strcpy (buf, "\r\n");
    cur = buf;
    StripCrLf(cur, &next);
    TEST_ASSERT (next != NULL);
    TEST_ASSERT (strcmp (cur, "") == 0);
    cur = next;
    StripCrLf (cur, &next);
    TEST_ASSERT (next == NULL);
}

TEST_CASE (TestTextDoc, test_CTextDocConstructor)
{
    CTextDoc doc;
    int i;

    TEST_ASSERT (doc.m_nValidLines == 1);
    TEST_ASSERT (doc.GetCount() == DOC_INIT_LINES);
    TEST_ASSERT (doc.GetArrayPtr()[0] != NULL);
    for (i = 1; i < DOC_INIT_LINES; i++) {
        TEST_ASSERT (doc.GetArrayPtr()[i] == NULL);
    }
}

TEST_CASE (TestTextDoc, test_CTextDocInserttextAt)
{
    CTextDoc doc;
    char buf[1024];

    int ret = doc.InsertTextAt(0, 0, "Hello world.");
    TEST_ASSERT (ret == 0);
    TEST_ASSERT (doc.m_nValidLines == 1);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "Hello world.") == 0);

    strcpy (buf, ", guy!\n\rThis is a cruel");
    ret = doc.InsertTextAt(0, 5, buf);
    TEST_ASSERT (ret == 0);
    TEST_ASSERT (doc.m_nValidLines == 2);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "Hello, guy!") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(1), "This is a cruel world.") == 0);

    strcpy(buf, "\r\nGood luck! ");
    ret = doc.InsertTextAt(LAST_LINE, EOL, buf);
    TEST_ASSERT (ret == 0);
    TEST_ASSERT (doc.m_nValidLines == 3);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "Hello, guy!") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(1), "This is a cruel world.") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(2), "Good luck! ") == 0);

    strcpy (buf, "\r\r\r\r\r\r");
    ret = doc.InsertTextAt(LAST_LINE, EOL, buf);
    TEST_ASSERT (ret == 0);
    TEST_ASSERT (doc.m_nValidLines == 9);

    strcpy (buf, "\n");
    ret = doc.InsertTextAt(LAST_LINE, EOL, buf);
    TEST_ASSERT (ret == 0);
    TEST_ASSERT (doc.m_nValidLines == 10);
    TEST_ASSERT (doc.GetCount() == 10);

    strcpy (buf, "\n");
    ret = doc.InsertTextAt(LAST_LINE, EOL, buf);
    TEST_ASSERT (ret == 0);
    TEST_ASSERT (doc.m_nValidLines == 11);
    TEST_ASSERT (doc.GetCount() == 20);

}

TEST_CASE (TestTextDoc, test_CompareTextPosition)
{
    TEST_ASSERT (CompareTextPosition (0, 0, 0, 0) == 0 );
    TEST_ASSERT (CompareTextPosition (LAST_LINE, EOL, LAST_LINE, EOL) == 0);
    TEST_ASSERT (CompareTextPosition (0, 0, 0, 1) < 0);
    TEST_ASSERT (CompareTextPosition (0, EOL, 1, 0) < 0);
    TEST_ASSERT (CompareTextPosition (1, 0, 0, EOL) > 0);
}

TEST_CASE (TestTextDoc, text_CTextDocRemoveText)
{
    CTextDoc doc;
    char buf[1024];

    strcpy (buf, "1abc\r2efg\r3hij\n4klm");
    doc.InsertTextAt(LAST_LINE, EOL, buf);
    doc.RemoveText(0, 1, 0, 1);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "1bc") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(1), "2efg") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(2), "3hij") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(3), "4klm") == 0);
    TEST_ASSERT (doc.m_nValidLines == 4);
    doc.RemoveText(1, 0, 1, EOL);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "1bc") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(1), "3hij") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(2), "4klm") == 0);
    TEST_ASSERT (doc.m_nValidLines == 3);
    doc.RemoveText(0, 1, LAST_LINE, EOL);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "1") == 0);
    TEST_ASSERT (doc.m_nValidLines == 1);
    doc.RemoveText(0,0, LAST_LINE, EOL);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "") == 0);
    TEST_ASSERT (doc.m_nValidLines == 1);

    strcpy (buf, "1abc\r2efg\r3hij\n4klm");
    doc.InsertTextAt (0, 0, buf);
    doc.RemoveText (2, EOL, 3, 0);
    TEST_ASSERT (doc.m_nValidLines == 3);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "1abc") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(1), "2efg") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(2), "3hijklm") == 0);
    strcpy (buf, "\r");
    doc.InsertTextAt(LAST_LINE, EOL, buf);
    TEST_ASSERT (strcmp (doc.GetLineText(2), "3hijklm") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(3), "") == 0);
    TEST_ASSERT (doc.m_nValidLines == 4);
    doc.RemoveText (2, 7, 2, EOL);
    TEST_ASSERT (doc.m_nValidLines == 3);
    TEST_ASSERT (strcmp (doc.GetLineText(2), "3hijklm") == 0);
    strcpy (buf, "\r5nop");
    doc.InsertTextAt (LAST_LINE, EOL, buf);
    TEST_ASSERT (doc.m_nValidLines == 4);
    TEST_ASSERT (strcmp (doc.GetLineText(3), "5nop") == 0);
    doc.RemoveText (0, 2, 2, 4);
    TEST_ASSERT (doc.m_nValidLines == 2);
    TEST_ASSERT (strcmp (doc.GetLineText(0), "1alm") == 0);
    TEST_ASSERT (strcmp (doc.GetLineText(1), "5nop") == 0);

    doc.RemoveText(0,0, LAST_LINE,EOL);
    strcpy (buf, "\r\r\r\r\r\r\r\r\r\r\r");
    doc.InsertTextAt (0,0, buf);
    TEST_ASSERT (doc.m_nValidLines == 12);
    TEST_ASSERT (doc.GetCount() == 20);
    doc.RemoveText(3,0, 5, EOL);
    TEST_ASSERT (doc.m_nValidLines == 9);
    TEST_ASSERT (doc.GetCount() == 10);

    doc.RemoveText(0,0, LAST_LINE,EOL);
    strcpy (buf, "abc\rdefg");
    doc.InsertTextAt (0,0, buf);
    doc.RemoveText(0, 3, 0, EOL);
    TEST_ASSERT (doc.m_nValidLines == 1);
    TEST_ASSERT (strcmp(doc.GetLineText(0), "abcdefg") == 0);
}

TEST_CASE (TestTextDoc, test_SetCrLf)
{
    CTextDoc doc;

    doc.SetCrLf("Hello");
    TEST_ASSERT (strcmp (doc.m_crlf, "He") == 0);
    doc.SetCrLf("T");
    TEST_ASSERT (strcmp (doc.m_crlf, "T") == 0);
}

TEST_CASE (TestTextDoc, test_GetTextLength)
{
    char buf[1024];
    CTextDoc doc;

    doc.SetCrLf("\r\n");
    strcpy (buf, "1abc!\r2efg@@\r3hij###\n4klm%%%%\n5nop^^^^^");

    doc.InsertTextAt (0,0, buf);

    size_t size = doc.GetTextLength (0,0, LAST_LINE, EOL);
    TEST_ASSERT (size == 43);

    strcpy (buf, "\r");
    doc.InsertTextAt(LAST_LINE, EOL, buf);
    size = doc.GetTextLength (0,0, 4, EOL);
    TEST_ASSERT (size == 45);

    size = doc.GetTextLength (0, 2, 4, 3);
    TEST_ASSERT (size == 36);

    size = doc.GetTextLength (3, 2, 3, 2);
    TEST_ASSERT (size == 1);
    TEST_ASSERT (doc.GetTextLength (3, 2, 3, 3) == 2);
    TEST_ASSERT (doc.GetTextLength (3, 2, 3, EOL) == 8);
}

TEST_CASE (TestTextDoc, test_GetText)
{
    char txt[1024];
    char buf[1024];
    CTextDoc doc;

    doc.SetCrLf("\r\n");
    strcpy (txt, "1abc!\r2efg@@\r3hij###\n4klm%%%%\n5nop^^^^^");
    doc.InsertTextAt (0,0, txt);

    size_t size = doc.GetTextLength (0,0, LAST_LINE, EOL);
    size = doc.GetText(0,0,LAST_LINE,EOL, buf, size+1);
    TEST_ASSERT (size == 43);
    TEST_ASSERT (strcmp(buf, "1abc!\r\n2efg@@\r\n3hij###\r\n4klm%%%%\r\n5nop^^^^^") == 0);

    strcpy (buf, "\r");
    doc.InsertTextAt(LAST_LINE, EOL, buf);
    size = doc.GetTextLength (0,0, 4, EOL);
    size = doc.GetText(0, 0, 4, EOL, buf, size+1);
    TEST_ASSERT (strcmp(buf, "1abc!\r\n2efg@@\r\n3hij###\r\n4klm%%%%\r\n5nop^^^^^\r\n") == 0);
    TEST_ASSERT (size == 45);

    size = doc.GetTextLength (0, 2, 4, 3);
    size = doc.GetText (0, 2, 4, 3, buf, size+1);
    TEST_ASSERT (strcmp(buf, "bc!\r\n2efg@@\r\n3hij###\r\n4klm%%%%\r\n5nop") == 0);
    TEST_ASSERT (size == 36);

    size = doc.GetTextLength (3, 2, 3, 2);
    size = doc.GetText(3,2,3,2, buf, size+1);
    TEST_ASSERT (size == 1);
    TEST_ASSERT (strcmp(buf, "l") == 0);

    size = doc.GetTextLength (3, 2, 3, 3);
    size = doc.GetText(3,2,3,3, buf, size+1);
    TEST_ASSERT (strcmp(buf, "lm") == 0);
    TEST_ASSERT (size == 2);

    size = doc.GetTextLength (3, 2, 3, EOL);
    size = doc.GetText(3,2,3,EOL, buf, size+1);
    TEST_ASSERT (strcmp(buf, "lm%%%%\r\n") == 0);
    TEST_ASSERT (size == 8);
}

TEST_CASE (TestTextDoc, test_read_write_file)
{
    CTextDoc doc;
    doc.SetCrLf("\r\n");

    doc.ReadFile("TestTextDoc.cpp");
    doc.WriteFile("Copy_of_TestTextDoc.cpp");
}

TEST_CASE (TestTextDoc, test_GetMaxLineLength)
{
    CTextDoc doc;
    doc.SetCrLf("\r\n");

    TEST_ASSERT (doc.GetMaxLineLength() == 0);
    doc.ReadFile("scanner_states.txt");
    TEST_ASSERT (doc.GetMaxLineLength() == 95);
}

TEST_CASE (TestTextDoc, test_TextDoc_FindInSelectionForward)
{
    char buf[1024];

    CTextDoc doc;
    doc.SetCrLf("\r\n");
    strcpy (buf, "1abc!\r2efg@@\r3hij###\n4klm%%%%\n5nop^^^^^");
    doc.InsertTextAt (0,0, buf);
    
    bool ret;
    CTextSelection sel, found;
    unsigned int sl, sc, bl, bc;

    sel.SetSelection (0, 0, LAST_LINE, EOL);
    ret = doc.FindInSelectionForward(&sel, "###", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc ==4 && bc == 7);

    sel.SetSelection (0, 1, LAST_LINE, EOL);
    ret = doc.FindInSelectionForward(&sel, "1", &found);
    TEST_ASSERT(!ret);

    sel.SetSelection (0, 1, LAST_LINE, 5);
    ret = doc.FindInSelectionForward(&sel, "^", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 4 && bl == 4);
    TEST_ASSERT (sc ==4 && bc == 5);

    sel.SetSelection (1, 2, 1, 5);
    ret = doc.FindInSelectionForward(&sel, "g", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 1 && bl == 1);
    TEST_ASSERT (sc == 3 && bc == 4);

    sel.SetSelection (1, 2, 2, 5);
    ret = doc.FindInSelectionForward(&sel, "g@", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 1 && bl == 1);
    TEST_ASSERT (sc == 3 && bc == 5);

    sel.SetSelection (1, 2, 2, 5);
    ret = doc.FindInSelectionForward(&sel, "hi", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc == 1 && bc == 3);

    sel.SetSelection (1, 2, 3, 5);
    ret = doc.FindInSelectionForward(&sel, "j##", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc == 3 && bc == 6);

    sel.SetSelection (1, 2, 3, 5);
    ret = doc.FindInSelectionForward(&sel, "nop", &found);
    TEST_ASSERT(!ret);
}

TEST_CASE (TestTextDoc, test_TextDoc_FindInSelectionBackward)
{
    char buf[1024];

    CTextDoc doc;
    doc.SetCrLf("\r\n");
    strcpy (buf, "1abc!\r2efg@@\r3hij###\n4klm%%%%\n5nop^^^^^");
    doc.InsertTextAt (0,0, buf);
    
    bool ret;
    CTextSelection sel, found;
    unsigned int sl, sc, bl, bc;

    sel.SetSelection (0, 0, LAST_LINE, EOL);
    ret = doc.FindInSelectionBackward(&sel, "###", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc ==4 && bc == 7);

    sel.SetSelection (0, 0, LAST_LINE, EOL);
    ret = doc.FindInSelectionBackward(&sel, "##", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc ==5 && bc == 7);

    sel.SetSelection (0, 1, LAST_LINE, EOL);
    ret = doc.FindInSelectionBackward(&sel, "1", &found);
    TEST_ASSERT(!ret);

    sel.SetSelection (0, 1, LAST_LINE, 5);
    ret = doc.FindInSelectionBackward(&sel, "^", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 4 && bl == 4);
    TEST_ASSERT (sc ==4 && bc == 5);

    sel.SetSelection (0, 1, LAST_LINE, 6);
    ret = doc.FindInSelectionBackward(&sel, "^", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 4 && bl == 4);
    TEST_ASSERT (sc ==5 && bc == 6);

    sel.SetSelection (1, 2, 1, 5);
    ret = doc.FindInSelectionBackward(&sel, "g", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 1 && bl == 1);
    TEST_ASSERT (sc == 3 && bc == 4);

    sel.SetSelection (1, 2, 2, 5);
    ret = doc.FindInSelectionBackward(&sel, "g@", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 1 && bl == 1);
    TEST_ASSERT (sc == 3 && bc == 5);

    sel.SetSelection (1, 2, 2, 5);
    ret = doc.FindInSelectionBackward(&sel, "hi", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc == 1 && bc == 3);

    sel.SetSelection (1, 2, 3, 5);
    ret = doc.FindInSelectionBackward(&sel, "j##", &found);
    TEST_ASSERT(ret);
    found.GetSelection(&sl, &sc, &bl, &bc);
    TEST_ASSERT (sl == 2 && bl == 2);
    TEST_ASSERT (sc == 3 && bc == 6);

    sel.SetSelection (1, 2, 3, 5);
    ret = doc.FindInSelectionBackward(&sel, "nop", &found);
    TEST_ASSERT(!ret);
}

/***************************** Test cases for CTextSelection ***********************/
TEST_CASE (TestTextDoc, test_setselection)
{
    CTextSelection sel;
    unsigned int l1, c1, l2, c2;
    
    sel.SetSelection (1, 10, 2, 3);
    sel.GetSelection (&l1, &c1, &l2, &c2);

    TEST_ASSERT (l1 == 1 && c1 == 10 && l2 == 2 && c2 == 3);

    sel.SetSelection (2, 3, 1, 10);
    sel.GetSelection (&l1, &c1, &l2, &c2);
    TEST_ASSERT (l1 == 2 && c1 == 3 && l2 == 1 && c2 == 10);

	sel.SetStart (3, 7);
    sel.GetSelection (&l1, &c1, &l2, &c2);
    TEST_ASSERT (l1 == 3 && c1 == 7 && l2 == 1 && c2 == 10);

	sel.SetEnd (7, 3);
    sel.GetSelection (&l1, &c1, &l2, &c2);
    TEST_ASSERT (l1 == 3 && c1 == 7 && l2 == 7 && c2 == 3);
}

TEST_CASE (TestTextDoc, test_inselection)
{
    CTextSelection sel;
    
    sel.SetSelection (3, 3, 1, 10);
    TEST_ASSERT (!sel.InSelection(0, EOL));
    TEST_ASSERT (!sel.InSelection(1, 9));
    TEST_ASSERT (sel.InSelection(1, 10));
    TEST_ASSERT (sel.InSelection(1, EOL));
    TEST_ASSERT (sel.InSelection(2, 0));
    TEST_ASSERT (sel.InSelection(2, EOL));
    TEST_ASSERT (sel.InSelection(3, 0));
    TEST_ASSERT (!sel.InSelection(3, 3));
    TEST_ASSERT (!sel.InSelection(3, 4));
    TEST_ASSERT (!sel.InSelection(3, EOL));
    TEST_ASSERT (!sel.InSelection(4, 0));
}

TEST_CASE(TestTextDoc, test_GetAscendantSel)
{
    CTextSelection sel;
    unsigned int sl, sc, bl, bc;

    sel.SetSelection (3, 3, 3, 3);
    TEST_ASSERT (!sel.GetAscendantSel(&sc, &sl, &bl, &bc));
    sel.SetSelection (3, EOL, 3, EOL);
    TEST_ASSERT (!sel.GetAscendantSel(&sc, &sl, &bl, &bc));

    sel.SetSelection (2, 0, 2, 1);
    TEST_ASSERT (sel.GetAscendantSel(&sl, &sc, &bl, &bc));
    TEST_ASSERT (sl == 2 && sc == 0);
    TEST_ASSERT (bl == 2 && bc == 1);

    sel.SetSelection (3, 0, 2, 1);
    TEST_ASSERT (sel.GetAscendantSel(&sl, &sc, &bl, &bc));
    TEST_ASSERT (sl == 2 && sc == 1);
    TEST_ASSERT (bl == 3 && bc == 0);
}


TEST_CASE (TestTextDoc, test_GetAscendantInclusiveSel)
{
    CTextSelection sel;
    unsigned int sl, sc, bl, bc;

    sel.SetSelection (3, 3, 3,3);
    TEST_ASSERT (!sel.GetAscendantInclusiveSel(&sl, &sc, &bl, &bc));
    sel.SetSelection (3, EOL, 3, EOL);
    TEST_ASSERT (!sel.GetAscendantInclusiveSel(&sl, &sc, &bl, &bc));

    sel.SetSelection (2, 0, 2, 1);
    TEST_ASSERT (sel.GetAscendantInclusiveSel(&sl, &sc, &bl, &bc));
    TEST_ASSERT (sl == 2 && sc == 0);
    TEST_ASSERT (bl == 2 && bc == 0);

    sel.SetSelection (3, 0, 2, 1);
    TEST_ASSERT (sel.GetAscendantInclusiveSel(&sl, &sc, &bl, &bc));
    TEST_ASSERT (sl == 2 && sc == 1);
    TEST_ASSERT (bl == 2 && bc == EOL);
}

/********************** Test for CSelections ************************/
TEST_CASE (TestTextDoc, test_AddSel_RemoveAll)
{
    CSelections selections;

    unsigned int sl, sc, bl, bc, i;

    sl = sc = 0;
    bl = bc = 10;
    CTextSelection* sel = NULL;
    for ( i = 0; i < SEL_INIT+1; i++) {    
        sel = new CTextSelection();
        sel->SetSelection(sl, sc, bl, bc);
        selections.AddSel(sel);
        sl ++;
        sc ++;
        bl ++;
        bc ++;
    }
    
    TEST_ASSERT (selections.GetCount() == SEL_INIT+SEL_GROW);
    TEST_ASSERT (selections.m_uSelCount == SEL_INIT+1);
    for (i = 0; i < SEL_INIT+1; i++) {
        sel = (selections.GetArrayPtr())[i];
        TEST_ASSERT (sel);
        sel->GetSelection(&sl, &sc, &bl, &bc);
        TEST_ASSERT (sl == i && sc == i && bl == 10+i && bc == 10+i);
    }
    selections.RemoveAll();
    TEST_ASSERT (selections.GetCount() == SEL_INIT);
    TEST_ASSERT (selections.m_uSelCount == 0);
}

TEST_GROUP(Global)
{
}

TEST_CASE(Global, testGetBlockMoveInsertPos)
{
    CTextSelection sel;

    sel.SetStart( 3, 2);
    sel.SetEnd(5,19);

    unsigned line, col;

    line = 0; col = 888;
    int ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 1);
    TEST_ASSERT (line == 0 && col == 888);

    line = 3; col = 2;
    ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 0);

    line = 3; col = 3;
    ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 0);
    line = 5; col = 18;
    ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 0);
    line = 5; col = 19;
    ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 0);

    line = 5; col = 20;
    ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 1);
    TEST_ASSERT (line == 3 && col == 3);

    line = 6; col = 20;
    ret = GetBlockMoveInsertPos (&line, &col, &sel);
    TEST_ASSERT (ret == 1);
    TEST_ASSERT (line == 4 && col == 20);
}
